/*
 * @author Jordi Mateo
 * @author Sergi Builtrago
 * @author Marc Gonzalez
 * @author Aitor Garcia
 * @version 23.12.2012
 */

package com.example.project;


import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Messenger;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;
import android.view.Window;


public class MainActivity extends Activity {
	
	
	 public static Messenger mService = null;
	 public static boolean mIsBound = false;
	 public static final String NOTIFICATION_DATA = "NOTIFICATION_DATA";
	 public static ServiceConnection mConnection = new ServiceConnection() 
	    {  	
	    	public void onServiceConnected(ComponentName className,IBinder service) 
	    	{
	    		mService = new Messenger(service);
	    		
	    	}
	    	
	    	public void onServiceDisconnected(ComponentName className) 
	    	{
	    		mService = null;
	    	}
	    };  

	
	
	public void onAttachedToWindow() {
        super.onAttachedToWindow();
        Window window = getWindow();
        window.setFormat(PixelFormat.RGBA_8888);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        createNotification(0, "servei corrent");
        CountDown _tik;
        // It delay the screen for 2.5 seconds and after that switch to YourNextActivity
        _tik=new CountDown(2500,2500,this,Splash_patrocinadores.class);
        _tik.start();
        //StartAnimations();
        
    }
    
    /*private void StartAnimations() {
        Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
        anim.reset();
        LinearLayout l=(LinearLayout) findViewById(R.id.lin_lay);
        l.clearAnimation();
        l.startAnimation(anim);
 
        anim = AnimationUtils.loadAnimation(this, R.anim.translate);
        anim.reset();
        ImageView iv = (ImageView) findViewById(R.id.logo);
        iv.clearAnimation();
        iv.startAnimation(anim);
 
    }*/

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    

    @Override
    public void onStart(){
        super.onStart();
        doBindService();
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        doUnbindService();
    }
   
    void doBindService() {
    	try {
    		bindService(new Intent(this,DateBaseService.class), mConnection, Context.BIND_AUTO_CREATE);

    	}
        catch (Exception e) {
            e.printStackTrace(); 
        }
        mIsBound = true;
    }
    
    void doUnbindService() {
        if (mIsBound) {
            unbindService(mConnection);
            mIsBound = false;
        }
    }
    

    
    private void createNotification(long when,String data){
    	
  
    	
		String notificationContent="";
		notificationContent=getString(R.string.service_run);
		String notificationTitle="";
		notificationTitle=getString(R.string.service_ready);
		
		//large icon for notification,normally use App icon
		Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
		int smalIcon =R.drawable.ic_launcher;
		String notificationData="This is data : "+data;
		
		/*create intent for show notification details when user clicks notification*/
		Intent intent =new Intent(getApplicationContext(), AccionServicio.class);
		intent.putExtra(NOTIFICATION_DATA, notificationData);
		
		/*create unique this intent from  other intent using setData */
		intent.setData(Uri.parse("content://"+when));
		/*create new task for each notification with pending intent so we set Intent.FLAG_ACTIVITY_NEW_TASK */
		PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
		
		/*get the system service that manage notification NotificationManager*/
		NotificationManager notificationManager =(NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); 
				
		/*build the notification*/
		NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
				getApplicationContext())
				.setWhen(when)
				.setContentText(notificationContent)
				.setContentTitle(notificationTitle)
				.setSmallIcon(smalIcon)
				.setAutoCancel(true)
				.setTicker(notificationTitle)
				.setLargeIcon(largeIcon)
				.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_VIBRATE| Notification.DEFAULT_SOUND)
				.setContentIntent(pendingIntent);
		
		/*Create notification with builder*/
		Notification notification=notificationBuilder.build();
		
		/*Fix Notification*/
		notification.flags=Notification.FLAG_ONGOING_EVENT;
		
		/*sending notification to system.Here we use unique id (when)for making different each notification
		 * if we use same id,then first notification replace by the last notification*/
		notificationManager.notify((int) when, notification);
	}
    
    
}
